#include "stdafx.h"
#include "Particle.h"
#include "..\..\EdgeGraphics\VertexBuffer.h"
#include "..\..\EdgeGraphics\ShaderContainer.h"
#include "..\..\EdgeGraphics\VertexFormats.h"
#include "..\..\EdgeMath\VectorHelper.h"
#include "..\..\EdgeContent\ContentManager.h"
#include "..\..\EdgeGraphics\Material.h"
#include "..\..\EdgeMath\Math.h"

using namespace VertexFormats;
using namespace VectorHelper;

/*static*/ VertexBuffer* Particle::vBuffer = nullptr;

/*static*/ ShaderContainer* Particle::shader_container = nullptr;

Particle::Particle(void)
{
	// create the vertex buffer if it has not been created
	if (!vBuffer)
	{
		// create vertecies
		VertexNormalTexture v[1] =
		{
			VertexNormalTexture(vector_3::Zero, vector_2::Zero, vector_3::Zero)
		};

		// create indicies
		unsigned int indicies[1] = { 0 };

		vBuffer = new VertexBuffer((void*)v, indicies, 1, 1, VERTEX_TYPE_POSITION_TEXCOORD_NORMAL);
		vBuffer->setTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
	}
	// create the particle shader if it has not been loaded
	if (!shader_container)
		shader_container = ContentManager::GetInstance()->LoadShaderPair("ParticleVShader.cso", "ParticlePShader.cso", "ParticleGShader.cso");
}

/*virtual*/ Particle::~Particle(void)
{
}

/*virtual*/ void Particle::setResourceView(IGraphicsResource* value, unsigned int index) /*override*/
{
	texture->textures.e[index] = value;
}

// update the particle
void Particle::Update(float dt)
{
	// update position
	velocity += acceleration * dt;
	position += velocity * dt;
	// update rotation
	rotation += rotation_speed * dt;

	// update lifetime
	lifetime -= dt;
	float ammount = 1.f - (lifetime / max_lifetime);

	// update alpha
	alpha = Math::lerp(start_alpha, end_alpha, ammount);
	// update scale
	size = Math::lerp(start_size, end_size, ammount);
	// update color
	color = VectorHelper::vector_4::Lerp(start_color, end_color, ammount);
}

